package qdu.rg012.draw_view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

public class Viewdraw extends Activity {
    Myview mview;
    Button btngo, btnstop;
    LinearLayout linearLy, btn_ly;
    int cx= 0,cy = 260, radius = 60;
    boolean flag = false;
    Myhandler mhandler;
    class Myhandler extends Handler{
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch(msg.what){
                case 13:

                    break;
            }
        }
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mhandler = new Myhandler();
        initUI();

        btngo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mview != null){
                    new Thread(){
                        public void run(){
                            flag = true;
                            while(flag){
                                Message msg = Message.obtain();
                                msg.what = 13;
                                if(mhandler != null)
                                    mhandler.sendMessage(msg);
//                                cx += 10;
//                                if(cx >= mview.getWidth() + radius/2)
//                                    cx = 0 - radius/2;
//                                mview.invalidate();
                                try {
                                    Thread.sleep(100);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }.start();
                }
            }
        });

        btnstop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                flag = false;
            }
        });

    }

    public void initUI(){
        mview = new Myview(this);
        linearLy = new LinearLayout(this);
        btn_ly = new LinearLayout(this);
        btngo = new Button(this); btnstop = new Button(this);

        linearLy.addView(btn_ly);
        btn_ly.setOrientation(LinearLayout.HORIZONTAL);
        btn_ly.addView(btngo); btn_ly.addView(btnstop);
        btn_ly.setWeightSum(2);
        linearLy.setOrientation(LinearLayout.VERTICAL);
        linearLy.addView(mview);

        LinearLayout.LayoutParams llpara = (LinearLayout.LayoutParams)btngo.getLayoutParams();
        llpara.width = 0;//ViewGroup.LayoutParams.WRAP_CONTENT;
        llpara.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        llpara.weight = 1;
        btngo.setLayoutParams(llpara);
        btngo.setText("go");
        llpara = (LinearLayout.LayoutParams)btnstop.getLayoutParams();
        llpara.width = 0;//ViewGroup.LayoutParams.WRAP_CONTENT;
        llpara.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        llpara.weight = 1;
        btnstop.setLayoutParams(llpara);
        btnstop.setText("stop");

        llpara = (LinearLayout.LayoutParams)mview.getLayoutParams();
        llpara.width = ViewGroup.LayoutParams.MATCH_PARENT;
        llpara.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        mview.setLayoutParams(llpara);

        setContentView(linearLy);
    }
    class Myview extends View {
        public Myview(Context context) {
            //构造函数参数获取所依附的组件
            super(context);
        }
        //绘制图形一般定义View的子类，在ondraw函数中绘制自定义图形
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            //该回调函数中可以自定义绘制图形
            //形参canvas即当前UI组件的画布
            canvas.drawColor(Color.YELLOW);
            Paint p = new Paint();//
            p.setColor(Color.RED);
            p.setStyle(Paint.Style.FILL);
            canvas.drawCircle(cx,cy,radius,p);
        }
    }
}
